package com.itheima.travel.web;

import com.github.pagehelper.PageInfo;
import com.itheima.travel.enums.StatusEnum;
import com.itheima.travel.exception.ProjectException;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.res.ResponseWrap;
import com.itheima.travel.service.RouteService;
import com.itheima.travel.utils.ExceptionsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("route")
@Log4j2
@Api(tags = "线路操作")
@CrossOrigin
public class RouteController {
    @Autowired
    RouteService routeService;

    @Value("${upLoad.webSite}")
    String webSite;

    @PostMapping
    @ApiOperation(value = "添加线路",notes = "添加线路")
    @ApiImplicitParam(name = "routeVo",value = "线路信息",required = true,dataType = "RouteVo")
    public ResponseWrap<Integer> addRoute(@RequestBody RouteVo routeVo) throws ProjectException {
        try {
            Integer flag = routeService.addRoute(routeVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("添加线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ADD_ROUTE_FAIL.getCode(),
                    StatusEnum.ADD_ROUTE_FAIL.getMsg());
        }
    }

    @PutMapping
    @ApiOperation(value = "修改线路",notes = "修改线路")
    @ApiImplicitParam(name = "routeVo",value = "线路信息",required = true,dataType = "RouteVo")
    public ResponseWrap<Integer> updateRoute(@RequestBody RouteVo routeVo) throws ProjectException {
        try {
            Integer flag = routeService.updateRoute(routeVo);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(flag)
                    .build();
        } catch (Exception e) {
            log.error("修改线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.UPDATE_ROUTE_FAIL.getCode(),
                    StatusEnum.UPDATE_ROUTE_FAIL.getMsg());
        }
    }

    @GetMapping("{routeId}")
    @ApiOperation(value = "按线路Id查询线路信息",notes = "按线路Id查询线路信息")
    @ApiImplicitParam(paramType = "path",name = "routeId",value = "线路id",required = true,dataType = "Long")
    public ResponseWrap<RouteVo> findRouteById(@PathVariable("routeId")Long routeId) throws ProjectException {
        RouteVo routeVo = RouteVo.builder().id(routeId).build();
        try {
            RouteVo routeResult = routeService.findRouteById(routeVo);
            return ResponseWrap.<RouteVo>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeResult)
                    .build();
        } catch (Exception e) {
            log.error("查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(
                    StatusEnum.FIND_ROUTE_ID_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_ID_FAIL.getMsg()
            );
        }
    }

    @PostMapping("{pageNum}/{pageSize}")
    @ApiOperation(value = "线路分页",notes = "线路分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "routeVo",value = "线路查询对象",required = false,dataType = "RouteVo"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",dataType = "Integer")
    })
    public ResponseWrap<PageInfo<RouteVo>> findRouteByPage(
        @RequestBody RouteVo routeVo,
        @PathVariable("pageNum") Integer pageNum,
        @PathVariable("pageSize") Integer pageSize
    ) throws ProjectException {
        try {
            PageInfo<RouteVo> routeByPage = routeService.findRouteByPage(routeVo, pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(routeByPage)
                    .build();
        } catch (Exception e) {
            log.error("分页查询线路：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_ROUTE_PAGE_FAIL.getCode(),
                    StatusEnum.FIND_ROUTE_PAGE_FAIL.getMsg());
        }
    }

}
